package com.jk.order.service.impl;

import com.jk.common.model.*;
import com.jk.common.model.OrderBean;
import com.jk.order.config.SnowflakeIdWorker;
import com.jk.order.mapper.OrderMapper;
import com.jk.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Autowired
    private OrderMapper orderMapper;




    @Override
    public List<ChinaAddressBean> getProviceBind(Integer pid) {

        return orderMapper.getProviceBind(pid);
    }

    @Override
    public void updateById(String orderNumber) {
        orderMapper.updateById(orderNumber);
    }

    //根据订单号查询订单
    @Override
    public OrderBean orderNumber(String orderNumber) {
        Query query = new Query();
        query.addCriteria(Criteria.where("orderNumber").is(orderNumber));
        OrderBean findOne = mongoTemplate.findOne(query, OrderBean.class);
        return findOne;
    }

    //根据订单修改订单状态
    @Override
    public void saveOrder(String orderNumber) {
        Query  query= new Query();
        query.addCriteria(Criteria.where("orderNumber").is(orderNumber));
        Update update = new Update();
        update.set("已支付",new OrderBean().getOrderStatus());
        mongoTemplate.updateFirst(query, update, OrderBean.class);
    }

    @Override
    public FlowerBean queryFlowerPrice(Integer flowerId) {

        return  orderMapper.queryFlowerPrice(flowerId);
    }

    @Override
    public void saveUserAddress(AddressBean addressBean) {
        orderMapper.saveUserAddress(addressBean);
    }

    @Override
    public Map generateOrderId(String userId, String flowerIds) {

     /*   String flowerIds = orderBean.getFlowerIds();
        String[] split = flowerIds.split(",");
        String str = "";
        for (int i = 0; i < split.length; i++) {
            int flowerId = Integer.parseInt(split[i]);
            String flowerName = orderMapper.findFlowerByFlowerId(flowerId);
            str += flowerName+",";
        }
        //生成订单id 并储存花的名称
        String flowerNames = str.substring(0, str.length() - 1);
        orderBean.setFlowerNames(flowerNames);*/
        HashMap<Object, Object> hashMap = new HashMap<>();
        long orderNumber2 = SnowflakeIdWorker.nextId();
        String orderNumber = String.valueOf(orderNumber2);

        orderMapper.savaOrderNumber(orderNumber,flowerIds,userId);
        hashMap.put("orderNumber",orderNumber);
        return hashMap;
    }

    @Override
    public void orderSave(OrderBean orderBean) {

         mongoTemplate.save(orderBean);

    }

    @Override
    public AddressBean findUserAddress(String userId) {

        return orderMapper.findUserAddress(userId);
    }


}
